import store from '../store';

const directives = {
  permission: {
    inserted(el , binding){
      let bindValue = binding.value;
      let code = typeof bindValue === 'object' ? (bindValue.key || bindValue.code || bindValue.btnKey || bindValue.btnCode || '') : (bindValue || '');
      if(!store.getters.permission[code]){
        // 开启后将对按钮(el-button)进行权限控制
        // el.parentNode.removeChild(el);
      }
    }
  }
}

export const directive = {
  install(Vue){
    Object.keys(directives).forEach(
      keyName => Vue.directive(keyName , directives[keyName])
    )
  }
}

export const clearDirective = (name , component , deep) => {
  if(component){
    if(component.directives && component.directives[name]){
      delete component.directives[name];
    }
    if(deep && component.components){
      Object.values(component.components).forEach(
        item => {
          clearDirective(name , item , deep);
        }
      )
    }
  }
}